﻿using System;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.OutWms
{
    public partial class PickBillDetailForm : BaseForm
    {
        #region 成员变量
        public BilOutPlanBill OutPlanBill { get; set; }
        public BilOutPlanBillDetail OutPlanBillDetail { get; set; }
        public BilOutBillDetail OutBillDetail { get; set; }
        #endregion

        #region 构造函数
        public PickBillDetailForm(BilOutPlanBill outPlanBill, BilOutPlanBillDetail outBillDetail)
        {
            InitializeComponent();
            OutPlanBill = outPlanBill;
            OutPlanBillDetail = outBillDetail;
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;

            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void InitControl()
        {
            //初始化库存信息
            StoGoodsStock queryRecord = new StoGoodsStock();
            queryRecord.GoodsCode = OutPlanBillDetail.GoodsCode;
            queryRecord.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            PagerIndex pageIndex = new PagerIndex();
            pageIndex.CurrentPageIndex = 1;
            pageIndex.PageSize = int.MaxValue;
            this.stoGoodsStockBindingSource.DataSource =
                client.QueryGoodsStockList(queryRecord, ref pageIndex);
            //初始化出库计划库单明细内容
            tbGoodsCode.Text = OutPlanBillDetail.GoodsCode;
            //-------------------------------------------------
            tbBarCode.Text = OutPlanBillDetail.GoodsBarCode; //???
            //--------------------------------------------------
            tbGoodsName.Text = OutPlanBillDetail.GoodsName;
            nudPackageQty.Value = 0;
            nudPieceQty.Value = 0;
            nudPackageQty.Maximum = Convert.ToDecimal(OutPlanBillDetail.GoodsPackageQty);
            nudPieceQty.Maximum = Convert.ToDecimal(OutPlanBillDetail.GoodsPieceQty);        
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbLocation.Text))
            {
                MessageBox.Show("请选择库位信息");
                return;
            }
            if (nudPackageQty.Value == 0 && nudPieceQty.Value == 0)
            {
                MessageBox.Show("整箱和零数不能同时为0！");
                return;
            }

            if (nudPackageQty.Value < OutPlanBillDetail.GoodsPackageQty ||
                nudPieceQty.Value < OutPlanBillDetail.GoodsPieceQty)
            {
                if (MessageBox.Show("库存数量小于发货计划数量，是否继续？", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
            }

            OutBillDetail = new BilOutBillDetail();
            //计划拣货的货物
            //货物信息
            OutBillDetail.GoodsCode = OutPlanBillDetail.GoodsCode;
            OutBillDetail.GoodsName = OutPlanBillDetail.GoodsName;
            OutBillDetail.GoodsSpec = OutPlanBillDetail.GoodsSpec;
            OutBillDetail.SingleGoodsCode = OutPlanBillDetail.SingleGoodsCode;
            OutBillDetail.StockCode = tbStoGoodsCode.Text;
            OutBillDetail.AreaCode = tbArea.Tag.ToString();
            OutBillDetail.AreaName = tbArea.Text;
            OutBillDetail.ShelfCode =tbShelf.Tag.ToString();
            OutBillDetail.ShelfName = tbShelf.Text;
            OutBillDetail.LocationCode = tbLocation.Tag.ToString();
            OutBillDetail.LocationName = tbLocation.Text;
            OutBillDetail.GoodsBarCode = tbBarCode.Text;
            OutBillDetail.StockCode = tbStoGoodsCode.Text;
            OutBillDetail.GoodsPackageQty = Convert.ToInt32(nudPackageQty.Value);
            OutBillDetail.GoodsPieceQty = Convert.ToInt32(nudPieceQty.Value);
            OutBillDetail.GoodsQty = OutBillDetail.GoodsPieceQty + OutBillDetail.GoodsPackageQty * OutBillDetail.GoodsSpec;
            //入库单信息

            //入库单中的货物数信息
            OutPlanBillDetail.GoodsPackageQty -= OutBillDetail.GoodsPackageQty;
            OutPlanBillDetail.GoodsPieceQty -= OutBillDetail.GoodsPieceQty;
            OutPlanBillDetail.GoodsQty -= OutBillDetail.GoodsQty;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void dgv_MouseClick(object sender, MouseEventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            StoGoodsStock selectedRecord = row.DataBoundItem as StoGoodsStock;
            tbPiecePrice.Text = selectedRecord.CostPrice.ToString();
            tbArea.Text = selectedRecord.AreaName;
            tbArea.Tag = selectedRecord.AreaCode;
            tbShelf.Text = selectedRecord.ShelfName;
            tbShelf.Tag = selectedRecord.ShelfCode;
            tbLocation.Text = selectedRecord.LocationName;
            tbLocation.Tag = selectedRecord.LocationCode;
            tbBarCode.Text = selectedRecord.GoodsBarCode;
            tbStoGoodsCode.Text = selectedRecord.Id;
            tbSingleGoodsCode.Text = selectedRecord.SingleGoodsCode;
            nudPackageQty.Maximum = selectedRecord.PackageQty;
            nudPieceQty.Maximum = selectedRecord.PieceQty;
            if (nudPackageQty.Maximum >= (OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty))
            {
                nudPackageQty.Maximum = OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty;
                nudPackageQty.Value = OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty;
            }
            else
            {
                nudPackageQty.Value = nudPackageQty.Maximum;
            }

            if (nudPieceQty.Maximum >= (OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty))
            {
                nudPieceQty.Maximum = OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty;
                nudPieceQty.Value = OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty;
            }
            else
            {
                nudPieceQty.Value = nudPieceQty.Maximum;
            }
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            StoGoodsStock selectedRecord = row.DataBoundItem as StoGoodsStock;
            tbArea.Text = selectedRecord.AreaName;
            tbArea.Tag = selectedRecord.AreaCode;
            tbShelf.Text = selectedRecord.ShelfName;
            tbShelf.Tag = selectedRecord.ShelfCode;
            tbLocation.Text = selectedRecord.LocationName;
            tbLocation.Tag = selectedRecord.LocationCode;
            tbBarCode.Text = selectedRecord.GoodsBarCode;
            tbStoGoodsCode.Text = selectedRecord.Id;
            tbSingleGoodsCode.Text = selectedRecord.SingleGoodsCode;
            nudPackageQty.Maximum = selectedRecord.PackageQty;
            nudPieceQty.Maximum = selectedRecord.PieceQty;
            if (nudPackageQty.Maximum >= (OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty))
            {
                nudPackageQty.Maximum = OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty;
                nudPackageQty.Value = OutPlanBillDetail.GoodsPackageQty - OutPlanBillDetail.OutGoodsPackageQty;
            }
            else
            {
                nudPackageQty.Value = nudPackageQty.Maximum;
            }

            if (nudPieceQty.Maximum >= (OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty))
            {
                nudPieceQty.Maximum = OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty;
                nudPieceQty.Value = OutPlanBillDetail.GoodsPieceQty - OutPlanBillDetail.OutGoodsPieceQty;
            }
            else
            {
                nudPieceQty.Value = nudPieceQty.Maximum;
            }
        }
        #endregion
    }
}
